<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use app\index\controller\BaseController;


class IndexController extends BaseController {
	protected function _initialize() {
		$url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$check = strpos($url, '?');
		if($check !== false){
			$url.="&fromuid=".session('user_id');
		}else{
			$url.="?fromuid=".session('user_id');
		}
 
		parent::_initialize();
		
		$ads_info = db('advertise')->select();
		$this->assign('ads', $ads_info);
		//$this -> auth_login();
	}
	//自定义分享设置
	function share_config(){
		return share_config();
	}
	public function sp_page(){
		//获取商品信息
		$good_info = db('goods')->where('id',input('goods_id'))->find();
		if(session("user_id")){
			if(session("user_info")['is_rz']==1){
				$good_info['price']=$good_info['sale_price2'];
			}else{
				$good_info['price']=$good_info['sale_price'];
			}
			$fx_id=session("user_id");
			$gurl=config('DOMAIN')."/index/index/sp_page?fx_id=$fx_id&goods_id=".input("goods_id");
			$this->assign('gurl',$gurl);
			
			$share_config = get_config('share_config');
			$this->assign('share_title',$share_config['share_spxq']);
			$this->assign('share_content',$share_config['share_spxq_content']);
			$this->assign('share_img',$share_config['spthumb']);
		}else{
			$good_info['price']=$good_info['sale_price'];
		}
		$this->assign('good_info',$good_info);
		return $this->fetch();
	}
	//产品中心
	public function index(){
		if(input("name")){
				$w['goods_name'] = ['like', '%'.input("name").'%'];
		}			
		$w['is_on_sale'] = 1;
		$w['state']=0;
		if($_POST){
			if(input("page")){
				$page=input("page");
			}
			$goods_info = db('goods')
								->field('id,goods_sn,goods_name,store,sale_price,thumb')
								->where($w)
								->order("sort desc")
								->page($page,20)
								->select();
			return json(['list'=>$goods_info]);
		}
		if(session("user_id")){
			$fx_id=session("user_id");
			$gurl=config('DOMAIN')."/index/index/index?fx_id=$fx_id";
			$this->assign('gurl',$gurl);
			
			$share_config = get_config('share_config');
			$this->assign('share_title',$share_config['share_cpzx']);
			$this->assign('share_content',$share_config['share_cpzx_content']);
			$this->assign('share_img',$share_config['cpthumb']);
		}
		$goods_info = db('goods')
								->field('id,goods_sn,goods_name,store,sale_price,thumb')
								->where($w)
								->order("sort desc")
								->limit(20)
								->select();
		return $this->fetch('',["list"=>$goods_info]);
	}	
	//会员服务
	public function hyfw(){
		if(session("user_id")){
			$fx_id=session("user_id");
			$gurl=config('DOMAIN')."/index/index/hyfw?fx_id=$fx_id";
			$this->assign('gurl',$gurl);
			
			$share_config = get_config('share_config');
			$this->assign('share_title',$share_config['share_hyfw']);
			$this->assign('share_content',$share_config['share_hyfw_content']);
			$this->assign('share_img',$share_config['hythumb']);
		}
		$user=db('member')->where("id",session('user_info')['id'])->field("is_rz,is_qy")->find();
		$this->assign('user',$user);
		$site_config = Db::name('system')->field('value')->where('name', 'service_config')->find();
		$site_config = unserialize($site_config['value']);
		$this->assign("site",$site_config);
		return $this->fetch();
	}
	//社交会所
	public function sjhs(){
		$page=1;
		if($_POST){
			if(input("page")){
				$page=input("page");
			}
			$begin = ($page-1)*20;
			$w['is_on_sale'] = 1;
			$home_info = db('beaut_home')->field('cost_price,id,home_name,sale_price,thumb,unit,desc')->where($w)->order("add_time desc")->limit($begin,20)->select();
			return json(['list'=>$home_info]);
		}
		if(session("user_id")){
			$fx_id=session("user_id");
			$gurl=config('DOMAIN')."/index/index/sjhs?fx_id=$fx_id";
			$this->assign('gurl',$gurl);
			
			$share_config = get_config('share_config');
			$this->assign('share_title',$share_config['share_sjhs']);
			$this->assign('share_content',$share_config['share_sjhs_content']);
			$this->assign('share_img',$share_config['hsthumb']);
		}
		return $this->fetch();
	}
	//会员体验
	public function hyty(){
		$user=db('member')->where("id",session('user_info')['id'])->field("is_rz")->find();
		$this->assign('user',$user);
		if(session("user_id")){
			$fx_id=session("user_id");
			$gurl=config('DOMAIN')."/index/index/hyty?fx_id=$fx_id";
			$this->assign('gurl',$gurl);
		}
		$site_config = Db::name('system')->field('value')->where('name', 'service_config')->find();
		 $site_config = unserialize($site_config['value']);
		$this->assign("site",$site_config);
		return $this->fetch();
	}
	//社交会所详情
	public function sjhs_page(){
		$home_info = db('beaut_home')->field('id,home_name,sale_price,cost_price,thumb,unit,content')->where('id',input('id'))->find();
		if(session("user_id")){
			$fx_id=session("user_id");
			$gurl=config('DOMAIN')."/index/index/hyty?fx_id=$fx_id&id=".input("id");
			$this->assign('gurl',$gurl);
		}
		$this->assign('list', $home_info);
		return $this->fetch();
	}
	
	
	//新增预定信息
	function add_reserve(){
		if(request()->isPost()){
			$order_sn = order_create_no();
			$data['home_id'] 	= input('home_id');
			//$data['state'] 		= 1;
			$data['re_name'] 	= input('re_name');
			$data['re_time'] 	= input('re_time');
			$data['re_people'] 	= input('re_people');
			$data['service'] 	= input('service');
			$data['re_type'] 	= input('re_type');
			$data['mobile'] 	= input('mobile');
			$data['user_id'] 	= session('user_id');		
			$data['order_sn'] 	= $order_sn;
			$data['balance'] 	= input('balance');
			$data['add_time'] 	= now_time();
			$result = db('reserve')->insert($data);
			if($result){
				return json(['code'=>200,'order_sn'=>$order_sn]);
			}else{
				return json(['code'=>403,'msg'=>'预定失败']);
			}
		}
	}
	
	function add_car(){
		//获取商品信息
		$goods_info = db('goods')->where('id',input('goods_id'))->find();
		
		if($goods_info){
			$in_cart = db('goods_car')->where('goods_id',$goods_info['id'])->where('user_id',session('user_id'))->find();
			
			if($in_cart){
				return json(['code'=>200,'url'=>url('index/showcar')]);
			}
			$data['user_id'] 		= session('user_id');
			$data['goods_id'] 		= $goods_info['id'];
			$data['goods_name'] 	= $goods_info['goods_name'];
			$data['goods_num'] 		= input('goods_num');
			$data['sale_price'] 	= $goods_info['sale_price'];
			$data['thumb'] 			= $goods_info['thumb'];
			$data['add_time'] 		= now_time();
			
			$result = db('goods_car')->insert($data);
			
			if($result){
				return json(['code'=>200,'url'=>url('index/showcar')]);
			}else{
				return json(['code'=>403,'msg'=>'加入购物车失败']);
			}
			
		}else{
			return json(['code'=>405,'msg'=>'数据异常商品不存在']);
		}
	}
	
	function showcar(){
		//获取购物车信息
		$car_list = db('goods_car')->where('user_id',session('user_id'))->select();
		
		
		$this->assign('car_list',$car_list);	
		
		return $this->fetch();
	}
	
	function del_cart_order(){
		$cart_id = input('cart_id');
		
		$result = db('goods_car')->where('id',$cart_id)->delete();
		if($result){
			return json(['code'=>200,'msg'=>'删除成功']);
		}else{
			return json(['code'=>403,'msg'=>'删除失败']);
		}
	}
	
	function add_cart_order(){
		$data = input('post.');
		
		$goods_arr = explode(",", $data['post_data']);
		$goods_list = array();
		
		foreach($goods_arr as $key=>$value){
			
			$goods_id = explode(":", $value);
			//获取商品信息
			$goods_info = db('goods')->where('id',$goods_id[0])->find();
			$goods_info['goods_num'] = $goods_id[1];
			$goods_list[] = $goods_info;
			
		}
		$result = true;
		Db::startTrans();
		
		$order_sn = order_create_no();
		/*================生成订单start=====================*/
		
		if(count($goods_list)>1){
			$type = 2;
		}else{
			$type = 1;
			$goods_list = $goods_list[0];
			
		}
		//新增订单
		$rest1  = $this->add_order($goods_list,$order_sn,$type);
		if(!$rest1) $result = false;
		
		$order_id = db('good_order')->getLastInsID();
		
		//订单详情
		$rest2 = $this->add_order_item($goods_list,$order_id,$type);
		if(!$rest2) $result = false;
		
		/*================生成订单end=====================*/
		
		$goods_ids = "";
		foreach($goods_arr as $key=>$value){
			
			$goods_id = explode(":", $value);
			$goods_ids .= $goods_id[0].",";
			
		}
		
		//删除购物车
		$rest3 = db('goods_car')
				->where('user_id',session('user_id'))
				->where('goods_id','in',substr($goods_ids,0,strlen($goods_ids)-1))
				->delete();
		if(!$rest3) $result = false;
		
		if($result){
			Db::commit();
			return json(['code'=>200,'msg'=>'添加订单成功','data'=>$order_id] );
		}else{
			Db::rollback();
			return json(['code'=>403,'msg'=>'添加订单失败']);
		}
	}
	
	
	
	function add_goods_order(){
		//获取商品信息
		$goods_info = db('goods')->where('id',input('goods_id'))->find();
		if($goods_info){
			$goods_info['goods_num'] = input('goods_num');
			$order_sn = order_create_no();
			$result = true;
			// 启动事务
			Db::startTrans();	
			//生成订单
			$rest1 = $this->add_order($goods_info, $order_sn,1);
			if(!$rest1) $result =false;
			$order_id = db('good_order')->getLastInsID();
			//生成订单详情
			$rest2 = $this->add_order_item($goods_info,$order_id,1);
			if(!$rest2) $result =false;
			
			if($result){
				Db::commit();
				return json(['code'=>200,'url'=>url('index/querdd',['order_id'=>$order_id])]);
			}else{
				Db::rollback();
				return json(['code'=>403,'msg'=>'数据异常']);
			}
		}else{
			return json(['code'=>403,'msg'=>'数据异常,商品不存在']);
		}
		
	}
	
	function querdd(){
		//获取用户地址
		$member_address = db('member')->where('id',session('user_id'))->find();
		//获取订单详情
		$order_items = db('order_items')->where('order_id',input('order_id'))->select();
		$total_money = 0;
		if($order_items){
			//计算商品金额
			foreach($order_items as $key=>$value){
				$total_money = bcadd($total_money, $value['total_price'], 2);
			}
		}else{
			return json(['code'=>403,'msg'=>'数据异常,订单不存在']);
		}
		$this->assign('member_address',$member_address);	
		$this->assign('order_id',input('order_id'));	
		$this->assign('total_money',$total_money);	
		$this->assign('order_items',$order_items);	
		return $this->fetch();
	}
	
/**
 * 	$goods_list 商品信息
 * 	$memo		订单留言
 *	$address_id 收货地址id
 * 	$order_type 付款类型
 * 
 * 生成订单，
 */
function add_order($goods_list,$order_sn,$type=1){
	//获取用户信息
	$user_info = db('member')->where('id',session('user_id'))->find(); 
	 if($user_info['is_rz']==1){
	 	$field="sale_price2";
	 }else{
	 	$field="sale_price";
	 }
	$total_price = 0;
	if ($type == 1) {
		$price = bcmul($goods_list[$field],$goods_list['goods_num'], 2);
		$total_price = bcadd($price, $total_price, 2);
		$tj_yj = bcmul($goods_list['hy1_balance'],$goods_list['goods_num'], 2);
		$tj_yj2 = bcmul($goods_list['hy2_balance'],$goods_list['goods_num'], 2);
		$pt_yj = bcmul($goods_list['pt_balance'],$goods_list['goods_num'], 2);
		$ct_yj = bcmul($goods_list['city_balance'],$goods_list['goods_num'], 2);
		
	}else{
		$tj_yj = 0;
		$pt_yj = 0;
		$tj_yj2 = 0;
		$ct_yj = 0;
		foreach($goods_list as $key=>$value){
			$price = bcmul($value[$field],$value['goods_num'], 2);
			$total_price = bcadd($price, $total_price, 2);
			
			$tj_yj_money = bcmul($value['hy1_balance'],$value['goods_num'], 2);
			$tj_yj = bcadd($tj_yj, $tj_yj_money, 2);
			
			$tj_yj2_money = bcmul($value['hy2_balance'],$value['goods_num'], 2);
			$tj_yj2 = bcadd($tj_yj2, $tj_yj2_money, 2);
			
			$pt_yj_money = bcmul($value['pt_balance'],$value['goods_num'], 2);
			$pt_yj = bcadd($pt_yj, $pt_yj_money, 2);
			
			
			$ct_yj_money = bcmul($value['city_balance'],$value['goods_num'], 2);
			$ct_yj = bcadd($ct_yj, $ct_yj_money, 2);
		}
	}
	
	$order_info['tj_yj'] 	= $tj_yj;
	$order_info['tj_id'] 	= $user_info['pid1'];
	
	$order_info['tj_yj2'] 	= $tj_yj2;
	$order_info['tj_id2'] 	= $user_info['pid2'];
	
	$order_info['pt_yj'] 	= $pt_yj;
	$order_info['pt_id'] 	= $user_info['pt_id'];
	
	$order_info['ct_yj'] 	= $ct_yj;
	$order_info['ct_id'] 	= $user_info['city_id'];
	
	$order_info['order_sn'] 	= $order_sn;
	$order_info['user_id'] 		= session('user_id');
	$order_info['pay_price'] 	= $total_price; 
	$order_info['add_time'] 	= now_time();
	
	return db('good_order')->insert($order_info);
}
	
//生成订单详情
function add_order_item($goods_list,$order_id,$type=1){
	$result =true;
	$user_info = db('member')->where('id',session('user_id'))->find(); 
	 if($user_info['is_rz']==1){
	 	$field="sale_price2";
	 }else{
	 	$field="sale_price";
	 }
	if ($type == 1) {
		
	    $total_price = bcmul($goods_list[$field],$goods_list['goods_num'], 2);
		
		$item_info['order_id'] 		= $order_id;
		$item_info['goods_id'] 		= $goods_list['id'];
		$item_info['nums'] 			= $goods_list['goods_num'];
		$item_info['total_price'] 	= $total_price;
		$item_info['price'] 		= $goods_list[$field];
		$item_info['goods_name'] 	= $goods_list['goods_name'];
		$item_info['thumb'] 		= $goods_list['thumb'];
		
		$rest =  db('order_items')->insert($item_info);
		if(!$rest)$result= false;
		
	} else {
	   foreach($goods_list as $key=>$value){
		   	$total_price = bcmul($value['sale_price'],$value['goods_num'], 2);
		   
		   	$item_info['order_id'] 		= $order_id;
			$item_info['goods_id'] 		= $value['id'];
			$item_info['nums'] 			= $value['goods_num'];
			$item_info['total_price'] 	= $total_price;
			$item_info['price'] 		= $value['sale_price'];
			$item_info['goods_name'] 	= $value['goods_name'];
			$item_info['thumb'] 		= $value['thumb'];
			
			$rest =  db('order_items')->insert($item_info);
			if(!$rest)$result= false;
	   }
	}
	
	return $result;
}
	
	//广告页面
	function adver(){
		$adver_info = db('advertise')->where('id',input("id"))->find();
		$this->assign('adver_info',$adver_info['content']);
		return $this->fetch();
	}
	
	
}
